package com.itheima.stock.controller;


import com.itheima.stock.pojo.domain.PermissionsTreeDomain;
import com.itheima.stock.pojo.domain.UserAddDomain;
import com.itheima.stock.pojo.domain.UserRoleDomain;
import com.itheima.stock.pojo.entity.SysPermission;
import com.itheima.stock.pojo.entity.SysRole;
import com.itheima.stock.pojo.entity.SysUser;
import com.itheima.stock.pojo.vo.*;
import com.itheima.stock.service.UserService;
import com.itheima.stock.vo.req.AddRoleVo;
import com.itheima.stock.vo.req.LoginReqVo;
import com.itheima.stock.vo.req.UpdateRoleVo;
import com.itheima.stock.vo.resp.LoginRespExpandVo;
import com.itheima.stock.vo.resp.LoginRespVo;
import com.itheima.stock.vo.resp.PageResult;
import com.itheima.stock.vo.resp.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api("/api")
@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据用户名称查询用户信息
     * @param name
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "userName", value = "", required = true)
    })
    @ApiOperation(value = "根据用户名查询用户信息", notes = "根据用户名称查询用户信息", httpMethod = "GET")
    @GetMapping("/user/{userName}")
    public SysUser getUserByUserName(@PathVariable("userName") String name){
        System.out.println(name);
        return userService.findByUserName(name);
    }

    /**
     *  转移到了security包下的SecurityConfig.java的JwtLoginAuthenticationFilter方法里了
     * @param vo
     * @return
     */
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "body", dataType = "LoginReqVo", name = "vo", value = "", required = true)
//    })
//    @ApiOperation(value = "用户登录", notes = "用户登录", httpMethod = "POST")
//    @PostMapping("/login")
//    public R<LoginRespExpandVo> login(@RequestBody LoginReqVo vo){
//        return userService.login(vo);
//    }

    /**
     * 生成图片验证码功能
     * @return
     */
    @ApiOperation(value = "生成图片验证码功能", notes = "生成图片验证码功能", httpMethod = "GET")
    @GetMapping("/captcha")
    public R<Map> getCaptchaCode(){
        return userService.getCaptchaCode();
    }



    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "UserSearchByPageInfo", name = "userSearchByPageInfo", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping ("/users")
    public R<PageResult<SysUser>> getUserInfoByPage(@RequestBody UserSearchByPageInfo userSearchByPageInfo){
        return userService.getUserInfoByPage(userSearchByPageInfo.getPageNum(),userSearchByPageInfo.getPageSize(),userSearchByPageInfo.getUsername(),userSearchByPageInfo.getNickName(),userSearchByPageInfo.getStartTime(),userSearchByPageInfo.getEndTime());
    }

    /**
     * 添加用户信息
     * @param userAddDomain
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "UserAddDomain", name = "userAddDomain", value = "", required = true)
    })
    @ApiOperation(value = "添加用户信息", notes = "添加用户信息", httpMethod = "POST")
    @PostMapping("/user")
    public R<String> addUser(@RequestBody UserAddDomain userAddDomain){
        return userService.addUser(userAddDomain);
    }

    /**
     * 获取用户具有的角色信息，以及所有角色信息
     * @param id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "userId", value = "", required = true)
    })
    @ApiOperation(value = "获取用户具有的角色信息，以及所有角色信息", notes = "获取用户具有的角色信息，以及所有角色信息", httpMethod = "GET")
    @GetMapping("/user/roles/{userId}")
    public R<UserRoleDomain> getUserRoleInfo(@PathVariable("userId") String id){
        return userService.getUserRoleInfo(id);
    }

    /**
     * 更新用户角色信息
     * @param userRolesUpdateInfo
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "UserRolesUpdateInfo", name = "userRolesUpdateInfo", value = "", required = true)
    })
    @ApiOperation(value = "更新用户角色信息", notes = "更新用户角色信息", httpMethod = "PUT")
    @PutMapping("/user/roles")
    public R<String> updateUserRole(@RequestBody UserRolesUpdateInfo userRolesUpdateInfo){
        return userService.updateUserRole(userRolesUpdateInfo);
    }

    /**
     * 批量删除用户
     * @param userIds
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "List<Long>", name = "userIds", value = "", required = true)
    })
    @ApiOperation(value = "批量删除用户", notes = "批量删除用户", httpMethod = "DELETE")
    @DeleteMapping("/user")
    public R<String> deleteUserByIds(@RequestBody List<Long> userIds){
        return userService.deleteUserByIds(userIds);
    }

    /**
     * 根据用户id查询用户信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "userId", value = "", required = true)
    })
    @ApiOperation(value = "根据用户id查询用户信息", notes = "根据用户id查询用户信息", httpMethod = "GET")
    @GetMapping("/user/info/{userId}")
    public R<SysUser> queryUserInfo(@PathVariable("userId")Long userId){
        return userService.getUserInfo(userId);
    }

    /**
     * 根据id更新用户基本信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysUser", name = "sysUser", value = "", required = true)
    })
    @ApiOperation(value = "根据id更新用户基本信息", notes = "根据id更新用户基本信息", httpMethod = "PUT")
    @PutMapping("/user")
    public R<String> updateUserById(@RequestBody SysUser sysUser){
        return userService.updateUserById(sysUser);
    }

    /**
     * 分页查询当前角色信息
     * @param page
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "PageVo", name = "page", value = "", required = true)
    })
    @ApiOperation(value = "分页查询当前角色信息", notes = "分页查询当前角色信息", httpMethod = "POST")
    @PostMapping("/roles")
    public R<PageResult<SysRole>> queryRolesByPage(@RequestBody PageVo page){
        return userService.queryRolesByPage(page.getPageNum(),page.getPageSize());
    }

    /**
     * 树状结构回显权限集合,底层通过递归获取权限数据集合
     * @return
     */
    @ApiOperation(value = "树状结构回显权限集合,底层通过递归获取权限数据集合", notes = "树状结构回显权限集合,底层通过递归获取权限数据集合", httpMethod = "GET")
    @GetMapping("/permissions/tree/all")
    public R<List<PermissionsTreeDomain> > getPermissionTree(){
        return userService.getAllPermissions();
    }

    /**
     * 添加角色和角色关联权限
     * @param addRoleVo
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "AddRoleVo", name = "addRoleVo", value = "", required = true)
    })
    @ApiOperation(value = "添加角色和角色关联权限", notes = "添加角色和角色关联权限", httpMethod = "POST")
    @PostMapping("/role")
    public R<String> addRoleConnection(@RequestBody AddRoleVo addRoleVo){
        return userService.addRoleConnection(addRoleVo);
    }

    /**
     * 根据角色id查找对应的权限id集合
     * @param id
     * @return
     */
    @GetMapping("/role/{roleId}")
    public R<List<Long>> getRolePermission(@PathVariable("roleId") Long id){
        return userService.getRolePermission(id);
    }

    /**
     * 更新角色信息，包含角色关联的权限信息
     * @param updateRoleVo
     * @return
     */
    @PutMapping("/role")
    public R<String> updateRolePermission(@RequestBody UpdateRoleVo updateRoleVo){
        return userService.updateRolePermission(updateRoleVo);
    }

    /**
     * 根据角色id删除角色信息
     * @param id
     * @return
     */
    @DeleteMapping("/role/{roleId}")
    public R<String> deleteRoleId(@PathVariable("roleId")Long id){
        return userService.deleteRoleById(id);
    }

    /**
     * 更新用户状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/role/{roleId}/{status}")
    public R<String> updateUserStatus(@PathVariable("roleId")Long id,@PathVariable("status")Integer status){
        return userService.updateUserStatus(id,status);
    }

    /**
     * 查询所有权限集合
     * @return
     */
    @GetMapping("/permissions")
    public R<List<SysPermission>> getPermissions(){
        return userService.getAllPermissionsNoOrder();
    }

    /**
     * 在添加权限菜单里以树状结构展示简略的权限信息。
     * @return
     */
    @GetMapping("/permissions/tree")
    public R<List<PermissionTreeNode>> showPermissionTree(){
        return userService.showPermissionTree();
    }

    /**
     * 添加权限
     * @param vo
     * @return
     */
    @PostMapping("/permission")
    public R<String> addPermission(@RequestBody PermissionAddVo vo){
        return userService.addPermission(vo);
    }

    /**
     * 更新权限
     * @param vo
     * @return
     */
    @PutMapping("/permission")
    public R<String> updatePermission(@RequestBody PermissionUpdateVo vo){
        return userService.updatePermission(vo);
    }

    /**
     * 根据id删除权限
     * @param id
     * @return
     */
    @DeleteMapping("/permission/{permissionId}")
    public R<String> deletePermission(@PathVariable("permissionId") Long id){
        return userService.deletePermissionById(id);
    }
}
